<?php
//
//echo "<br> ... request...<br>";
//print_r($_REQUEST);
//
//echo "<br> ... post...<br>";
//print_r($_POST);
//
//echo "<br> ... files...<br>";
//print_r($_FILES);
//

//echo "<br>".$UploaderID=trim($_POST["X_UPLOADER"]);
//echo "<br>".$UploadPostId=trim($_POST["X_ID"]);
    
if(isset($_POST["X_ID"]) && isset($_POST["X_UPLOADER"]) ){
    $UploaderID=trim($_POST["X_UPLOADER"]);
    $UploadPostId=trim($_POST["X_ID"]);
    $uploaderFileControlName="uploadcmntfile";
    
    if(isset($_FILES) && trim($_FILES[$uploaderFileControlName]["error"])==0 && $_FILES[$uploaderFileControlName] > 0 && strstr($_FILES[$uploaderFileControlName]["type"],"image/")){
        $filepathToMove="uploads/commentsmedia/temp";
        $fileName=$UploadPostId."-".$UploaderID.".".pathinfo($_FILES[$uploaderFileControlName]["name"], PATHINFO_EXTENSION);
        $filePath=$filepathToMove."/".$fileName;
        $movedTempPath=$_FILES[$uploaderFileControlName]["tmp_name"];
        
        $isPathExist=false;
        if(file_exists($filepathToMove)){
            $isPathExist=true;
        }
        else{
            mkdir($filepathToMove, 0777, true);
            
        }
        if(file_exists($filepathToMove)){
            $isPathExist=true;
        }
        
        if($isPathExist){
            if(move_uploaded_file($_FILES[$uploaderFileControlName]["tmp_name"],$filePath)){
                $movedTempPath=$filePath;
            }
        }
        $arr = array('errCode' => 200, 'postId' => $UploadPostId, 'uploaderID' => $UploaderID, 'uploadPath' => $movedTempPath, 'msg' => 'uploaded successfully');
        //$jsonval.="{\"name\":\"".$_FILES["userfile"]["name"]."\",\"type\":\"".str_replace("\\","\/",$_FILES["userfile"]["type"]."\",\"tmp_name\":\"".$_FILES["userfile"]["tmp_name"])."\",\"size\":\"".$_FILES["userfile"]["size"]."\"}";    
    }
    else{
        $arr = array('errCode' => 400, 'postId' => $UploadPostId, 'uploaderID' => $UploaderID, 'uploadPath' => '', 'msg' => 'some error occurred'.$_FILES[$uploaderFileControlName]["error"]);
    }
    
}
else{
    $arr = array('errCode' => 500, 'postId' => -1, 'uploaderID' => -1, 'uploadPath' => '', 'msg' => 'arguments missing or invalid request');
}
echo json_encode($arr);

?>